package com.csun.nusing.ui.fragment;

import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.csun.nusing.R;
import com.csun.nusing.bean.jsonbean.AlertShowAllJsonBean.AlertShowAllDataBean.AlertShowAllListBean;
import com.csun.nusing.familypresenter.FragmentFamilyAlarmPresenter;
import com.csun.nusing.presenter.inter.INursingOldrest;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.Calendar;

/**
 * 家属 ClassName: FamilyAlarmFragment
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-11-6
 */
public class FamilyAlarmFragment extends
		BaseFragment<FragmentFamilyAlarmPresenter> implements INursingOldrest {

	private ImageView searchiv;
	private PullToRefreshListView listview;
	private RelativeLayout other_rl;
	private TextView other_tv;
	private RelativeLayout state_rl;
	private RelativeLayout time_rl;
	private RelativeLayout type_rl;
	private TextView state_tv;
	private TextView time_tv;
	private TextView type_tv;
	private String isReadFalg = "";
	private String aletType = "";
	private DatePicker starttimedp;
	private DatePicker endtimedp;
	private int startyear;
	private int startmonth;
	private int startday;
	private String starttime = "";
	private int endyear;
	private int endmonth;
	private int endday;
	private String endtime = "";

	public FamilyAlarmFragment() {
	}

	public static FamilyAlarmFragment newInstance() {
		return new FamilyAlarmFragment();
	}

	@Override
	public int getContentViewId() {
		return R.layout.fragment_family_alarm;
	}

	// findView
	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView(view);
	}

	/**
	 * 控件
	 * 
	 * @Description: TODO
	 * @param @param view
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-20
	 */
	private void findView(View view) {
		other_rl = view
				.findViewById(R.id.fragment_family_alarm_top_other_rl);
		other_tv = view
				.findViewById(R.id.fragment_family_alarm_top_other_tv);
		state_rl = view
				.findViewById(R.id.fragment_family_alarm_top_state_rl);
		time_rl = view
				.findViewById(R.id.fragment_family_alarm_top_time_rl);
		type_rl = view
				.findViewById(R.id.fragment_family_alarm_top_type_rl);
		state_tv = view
				.findViewById(R.id.fragment_family_alarm_top_state_tv);
		time_tv = view
				.findViewById(R.id.fragment_family_alarm_top_time_tv);
		type_tv = view
				.findViewById(R.id.fragment_family_alarm_top_type_tv);

		listview = view
				.findViewById(R.id.fragment_family_alarm_listview);

		other_rl.setOnClickListener(new click());
		state_rl.setOnClickListener(new click());
		time_rl.setOnClickListener(new click());
		type_rl.setOnClickListener(new click());

		listview.setMode(Mode.BOTH);
		listview.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> arg0) {
				mPresenter.getOldListDate(getActivity(), listview, isReadFalg,
						aletType, starttime, endtime);
			}
		});

	}

	// 交给presenter处理数据逻辑
	@Override
	public void initData() {
		super.initData();
		Calendar c = Calendar.getInstance();
		startyear = c.get(Calendar.YEAR);
		c.add(Calendar.MONTH, 1);
		startmonth = c.get(Calendar.MONTH);
		startday = 1;
		endyear = c.get(Calendar.YEAR);
		endmonth = c.get(Calendar.MONTH);
		endday = c.get(Calendar.DAY_OF_MONTH);
		starttime = startyear + "-" + startmonth + "-" + startday + " 00:00:00";
		endtime = endyear + "-" + endmonth + "-" + endday + " 23:59:59";
		mPresenter.getOldListDate(getActivity(), listview, isReadFalg,
				aletType, starttime, endtime);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	protected FragmentFamilyAlarmPresenter getPresenter() {
		return new FragmentFamilyAlarmPresenter(this);
	}

	/**
	 * 将数据及点击事件回调出来 跳转下层
	 */
	@Override
	public void clickBtn(AlertShowAllListBean alertShowAllListBean) {
		// 点击btn的回调事件
	}

	/**
	 * 所以点击事件' ClassName: click
	 * 
	 * @Description: TODO
	 * @author fzq
	 * @date 2017-11-1
	 */
	private class click implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.fragment_family_alarm_top_state_rl:
				// 弹出状态的pop
				showPopState(v);
				break;
			case R.id.fragment_family_alarm_top_time_rl:
				// 弹出时间的pop
				showtimeState(v);
				break;
			case R.id.fragment_family_alarm_top_type_rl:
				// 弹出状态的pop
				showtypeState(v);
				break;
			case R.id.fragment_family_alarm_top_other_rl:
				// 弹出状态的pop
				showotherState(v);
				break;

			default:
				break;
			}
		}
	}

	/**
	 * 设置添加屏幕的背景透明度
	 * 
	 * @param bgAlpha
	 */
	public void backgroundAlpha(float bgAlpha) {
		WindowManager.LayoutParams lp = getActivity().getWindow()
				.getAttributes();
		lp.alpha = bgAlpha; // 0.0-1.0
		getActivity().getWindow().setAttributes(lp);
	}

	// 弹出状态的pop
	public void showPopState(View v) {
		View contentView = LayoutInflater.from(getActivity()).inflate(
				R.layout.pop_family_alarm_state, null);
		PopupWindow mPopupWindow = new PopupWindow(getActivity());
		mPopupWindow.setContentView(contentView);
		mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setTouchable(true);
		mPopupWindow.setOutsideTouchable(true);
		backgroundAlpha(0.7f);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(getResources().getColor(
				R.color.white));
		mPopupWindow.setBackgroundDrawable(dw);
		RadioGroup staterg = contentView
				.findViewById(R.id.pop_family_alarm_state_rg);
		if (isReadFalg == "") {
			staterg.check(R.id.pop_family_alarm_state_all_rb);
		} else if (isReadFalg == "0") {
			staterg.check(R.id.pop_family_alarm_state_ng_rb);
		} else if (isReadFalg == "1") {
			staterg.check(R.id.pop_family_alarm_state_ok_rb);
		}
		staterg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.pop_family_alarm_state_all_rb:
					isReadFalg = "";
					state_tv.setText(getString(R.string.chuli_state));
					break;
				case R.id.pop_family_alarm_state_ok_rb:
					isReadFalg = "1";
					state_tv.setText(getString(R.string.resolved));
					break;
				case R.id.pop_family_alarm_state_ng_rb:
					isReadFalg = "0";
					state_tv.setText(getString(R.string.un_resolve));
					break;
				default:
					break;
				}
			}
		});
		// 显示在view下方
		mPopupWindow.showAsDropDown(v);
		mPopupWindow.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				mPresenter.getOldListDate(getActivity(), listview, isReadFalg,
						aletType, starttime, endtime);
				backgroundAlpha(1.0f);
			}
		});
	}

	// 弹出时间的pop
	public void showotherState(View v) {

	}

	// 弹出状态的pop
	public void showtypeState(View v) {
		View contentView = LayoutInflater.from(getActivity()).inflate(
				R.layout.pop_family_alarm_type, null);
		PopupWindow mPopupWindow = new PopupWindow(getActivity());
		mPopupWindow.setContentView(contentView);
		mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setTouchable(true);
		mPopupWindow.setOutsideTouchable(true);
		backgroundAlpha(0.7f);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(getResources().getColor(
				R.color.white));
		mPopupWindow.setBackgroundDrawable(dw);
		RadioGroup typerg = contentView
				.findViewById(R.id.pop_family_alarm_type_rg);
		if (aletType == "") {
			typerg.check(R.id.pop_family_alarm_type_all_rb);
		} else if (aletType == "0") {
			typerg.check(R.id.pop_family_alarm_type_ng_rb);
		} else if (aletType == "1") {
			typerg.check(R.id.pop_family_alarm_type_ok_rb);
		}
		typerg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.pop_family_alarm_type_all_rb:
					aletType = "";
					type_tv.setText(getString(R.string.all_alarm));
					break;
				case R.id.pop_family_alarm_type_ok_rb:
					aletType = "1";
					type_tv.setText(getString(R.string.location_alarm));
					break;
				case R.id.pop_family_alarm_type_ng_rb:
					aletType = "0";
					type_tv.setText(getString(R.string.emergent_alarm));
					break;
				default:
					break;
				}
			}
		});
		// 显示在view下方
		mPopupWindow.showAsDropDown(v);
		// 显示在view下方
		mPopupWindow.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				mPresenter.getOldListDate(getActivity(), listview, isReadFalg,
						aletType, starttime, endtime);
				backgroundAlpha(1.0f);
			}
		});
	}

	// 弹出时间的pop
	public void showtimeState(View v) {
		View contentView = LayoutInflater.from(getActivity()).inflate(
				R.layout.pop_family_twotime_select_ll, null);
		PopupWindow mPopupWindow = new PopupWindow(getActivity());
		mPopupWindow.setContentView(contentView);
		mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
		mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
		mPopupWindow.setTouchable(true);
		mPopupWindow.setOutsideTouchable(true);
		backgroundAlpha(0.7f);
		// 实例化一个ColorDrawable颜色为半透明
		ColorDrawable dw = new ColorDrawable(getResources().getColor(
				R.color.white));
		mPopupWindow.setBackgroundDrawable(dw);

		starttimedp = contentView
				.findViewById(R.id.pop_twotime_starttime_dp);

		endtimedp = contentView
				.findViewById(R.id.pop_twotime_endtime_dp);

		starttimedp.init(startyear, startmonth - 1, startday,
				new OnDateChangedListener() {

					@Override
					public void onDateChanged(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						startyear = year;
						startmonth = monthOfYear + 1;
						startday = dayOfMonth;
						starttime = startyear + "-" + startmonth + "-"
								+ startday + " 00:00:00";
						// 将起始时间小于等于结束时间
						if (startyear > endyear) {
							startyear = endyear;
						}
						if (startyear == endyear) {
							if (startmonth > endmonth) {
								startmonth = endmonth;
							}
							if (startmonth == endmonth) {
								if (startday > endday) {
									startday = endday;
								}
							}
						}
					}
				});

		endtimedp.init(endyear, endmonth - 1, endday,
				new OnDateChangedListener() {

					@Override
					public void onDateChanged(DatePicker view, int year,
							int monthOfYear, int dayOfMonth) {
						endyear = year;
						endmonth = monthOfYear + 1;
						endday = dayOfMonth;
						endtime = endyear + "-" + endmonth + "-" + endday
								+ " 23:59:59";
					}
				});
		// 显示在view下方
		mPopupWindow.showAsDropDown(v);
		// 显示在view下方
		mPopupWindow.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				mPresenter.getOldListDate(getActivity(), listview, isReadFalg,
						aletType, starttime, endtime);
				backgroundAlpha(1.0f);
			}
		});
	}

}
